package com.stone.design.practice.customview.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author Stone
 * @version V1.0
 * @description: 自定义view学习
 * @date 4/14/18 09:49
 * @email redsstone@163.com
 */
public class MyView extends View{


    private Paint mPaint = new Paint();
    private int mWidth;
    private int mHeight;
    private int startX;
    private int startY;

    public MyView(Context context) {
        super(context);
        //this(context, null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //this(context, attrs, 0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {


        super(context, attrs, defStyleAttr);
        initPaint();
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);

        //region
       /* if (width < height) {
            height = width;
        } else {
            width = height;
        }*/
        /*int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        String mode = Integer.toBinaryString(widthMode);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);


        Log.e("Stone", "onMeasure(MyView.java:43)-->>widthMeasureSpec："+Integer.toBinaryString(-2147483648));
        Log.e("Stone", "onMeasure(MyView.java:45)-->>heightMeasureSpec："+Integer.toBinaryString(MeasureSpec.AT_MOST));
        if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(50, 100);
        }
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);*/
        //endregion
        setMeasuredDimension(width, height);
    }

    /**
     * 确定view的大小
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        //region
        //canvas.drawColor(Color.WHITE);

        /*// 绘制点
        canvas.drawPoint(200, 200, mPaint);
        canvas.drawPoints(new float[] {
              500, 500,
              500, 600,
              500, 700
        }, mPaint);

        // 绘制直线

        canvas.drawLine(300, 300, 800, 300, mPaint);
        canvas.drawLine(300, 300, 800, 800, mPaint);

        // 矩形
        canvas.drawRect(300, 300, 500, 800, mPaint);

        RectF rectF = new RectF(600, 300, 800, 600);
        //canvas.drawRect(rectF, mPaint);
        canvas.drawRoundRect(rectF, 30, 30, mPaint);

        canvas.drawCircle(200, 900, 100, mPaint);*/

        /*RectF rectF = new RectF(100,100,800,800);
        // 绘制背景矩形
        mPaint.setColor(Color.BLACK);*/
        //canvas.drawCircle(500, 500, 500, mPaint);

        /*mPaint.setColor(Color.GRAY);
        canvas.drawArc(rectF,0,160,true,mPaint);*/

        /*RectF rectF2 = new RectF(100,600,800,900);
        // 绘制背景矩形
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF2,mPaint);

        // 绘制圆弧
        mPaint.setColor(Color.BLUE);
        canvas.drawArc(rectF2,0,90,true,mPaint);
*/

        /*mPaint.setColor(Color.BLACK);
        canvas.translate(200, 200);
        canvas.drawCircle(0, 0, 100, mPaint);

        mPaint.setColor(Color.BLUE);
        canvas.translate(200, 200);
        canvas.drawCircle(0, 0, 100, mPaint);*/

        /*canvas.translate(mWidth/2, mHeight/2);
        mPaint.setColor(Color.BLACK);
        RectF rectF = new RectF(0, -400, 400, 0);
        canvas.drawRect(rectF, mPaint);

        canvas.scale(-0.5f, 0.5f, 200, 0);

        mPaint.setColor(Color.BLUE);
        canvas.drawRect(rectF, mPaint);

        canvas.rotate(180, 200, 0);
        canvas.drawRect(rectF, mPaint);*/

        /*mPaint.setColor(Color.BLUE);
        canvas.translate(mWidth/2, mHeight/2);
        canvas.drawCircle(0, 0, 400, mPaint);
        canvas.drawCircle(0, 0, 350, mPaint);
        for (int i = 0; i < 360; i+=10) {
            canvas.drawLine(0,350, 0, 400, mPaint);
            canvas.rotate(10);
        }*/
        //endregion



    }

    private void initPaint() {
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2f);
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
    }

    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {
                mySize = size;
                break;
            }

            case MeasureSpec.EXACTLY: {
                mySize = size;
                break;
            }

            case MeasureSpec.AT_MOST: {
                mySize = defaultSize;
                break;
            }

            default:
                break;
        }

        return mySize;
    }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

       Log.e("Stone1", "onTouchEvent(MyView.java:223)-->>"+".................");

        int x = (int) event.getX();
        int y = (int) event.getY();

      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              startX = x;
              startY = y;
              Log.e("Stone1", "onTouchEvent(MyView.java:223)-->>"+"down");
              break;
          }

          case MotionEvent.ACTION_MOVE: {

              Log.e("Stone1", "onTouchEvent(MyView.java:223)-->>"+"move");
              //计算移动的距离
              int offsetX = x - startX;
              int offsetY = y - startY;
              
              /*layout(getLeft()+offsetX, getTop()+offsetY,
                      getRight()+offsetX , getBottom()+offsetY);*/

              ((View)getParent()).scrollBy(-offsetX,-offsetY);
              break;
          }
      }

      Log.e("Stone", "onTouchEvent(MyView.java:207)-->>"+event.getAction());
    return true;
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
      Log.e("Stone", "dispatchTouchEvent(MyView.java:213)-->>"+event.getAction());
    return super.dispatchTouchEvent(event);
  }

  private void init(Context context) {
  }
}
